package com.apac.controllers;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jmesa.limit.ExportType;
import org.jmesa.model.TableModel;
import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;
import org.jmesa.view.html.event.RowEvent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.apac.bom.TestObject;
import com.apac.dao.TestDao;

@Controller
@RequestMapping("/assets")
public class AssetMonitoringController {

	@RequestMapping(value="", method=RequestMethod.GET)
	public ModelAndView assetMonitoring(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("AssetMonitoring");
		
		String deployed = generateDeployedAssets(request, response);
		String repair = generateForRepairAssets(request, response);
		String stock = generateOnStockAssets(request, response);
		
		mv.addObject("deployed", deployed);
		mv.addObject("repair", repair);
		mv.addObject("stock", stock);
		return mv;
	}
	
	public String generateDeployedAssets(HttpServletRequest request, HttpServletResponse response){
		/**MOCK DATA**/
		ArrayList<TestObject> data = new ArrayList<TestObject>();
		TestDao tDao = new TestDao();
		data = tDao.retrieveAll();
		/**MOCK DATA**/
		
		TableModel tableModel = new TableModel("deployed", request, response);
		
		tableModel.setExportTypes(ExportType.CSV,ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(data);
		
		if(tableModel.isExporting()){
			Table Table = new Table().caption("Assets Record");
			
			Row Row = new Row();
			Table.setRow(Row);
			
			Column col1 = new Column("id").title("No.");
			Row.addColumn(col1);
	
			Column col2 = new Column("att1").title("Asset Tag");
			Row.addColumn(col2);
	
//			Column col3 = new Column("att2").title("Adapter Tag");
//			Row.addColumn(col3);
	
			Column col4 = new Column("att3").title("Serial No.");
			Row.addColumn(col4); 
	
			Column col5 = new Column("att1").title("Description");
			Row.addColumn(col5);
	
			Column col6 = new Column("att2").title("Assignee");
			Row.addColumn(col6);
	
			Column col7 = new Column("att3").title("Project Name");
			Row.addColumn(col7); 
	
			Column col8 = new Column("att1").title("Issue Date");
			Row.addColumn(col8);
	
			Column col9 = new Column("att2").title("Return");
			Row.addColumn(col9);
	
			Column col10 = new Column("att1").title("Status");
			Row.addColumn(col10);
	
			Column col11 = new Column("att2").title("Remarks");
			Row.addColumn(col11);
	
			tableModel.setTable(Table);
			
			String export = tableModel.render();
			return export;
		}
		else{
			HtmlTable htmlTable = new HtmlTable();
	
			HtmlRow HtmlRow = new HtmlRow();
			HtmlRow.setOnclick(new RowEvent() {
	            public String execute(Object item, int rowcount) {
	                return "alert('Object" +item.toString()+ " Rowcount"+ rowcount +"')";
	            }
	        });
			htmlTable.setRow(HtmlRow);
						
			HtmlColumn col1 = new HtmlColumn("id").title("No.");
			HtmlRow.addColumn(col1);
	
			HtmlColumn col2 = new HtmlColumn("att1").title("Asset Tag");
			HtmlRow.addColumn(col2);
	
//			HtmlColumn col3 = new HtmlColumn("att2").title("Adapter Tag");
//			HtmlRow.addColumn(col3);
	
			HtmlColumn col4 = new HtmlColumn("att3").title("Serial No.");
			HtmlRow.addColumn(col4); 
	
			HtmlColumn col5 = new HtmlColumn("att1").title("Description");
			HtmlRow.addColumn(col5);
	
			HtmlColumn col6 = new HtmlColumn("att2").title("Assignee");
			HtmlRow.addColumn(col6);
	
			HtmlColumn col7 = new HtmlColumn("att3").title("Project Name");
			HtmlRow.addColumn(col7); 
	
			HtmlColumn col8 = new HtmlColumn("att1").title("Issue Date");
			HtmlRow.addColumn(col8);
	
			HtmlColumn col9 = new HtmlColumn("att2").title("Return");
			HtmlRow.addColumn(col9);
	
			HtmlColumn col10 = new HtmlColumn("att1").title("Status");
			HtmlRow.addColumn(col10);
	
			HtmlColumn col11 = new HtmlColumn("att2").title("Remarks");
			HtmlRow.addColumn(col11);
	    	
//	    	HtmlColumn viewLink = new HtmlColumn("id").title("View");
//	    	viewLink.setCellEditor(new CellEditor() {             
//				public Object getValue(Object item, String property, int rowcount) {                 
//	    			//Object value = new HtmlCellEditor().getValue(item, property, rowcount);                 
//	    			HtmlBuilder html = new HtmlBuilder();
//	    			ExamResponse er = (ExamResponse)item;
//	    			html.a().href().quote().append("JMesaExportStudentExamServlet?id="+er.getExamResponseID()).quote().close();
//	    			html.append("View Response");
//	    			html.aEnd();
//	    			return html.toString();             
//	    			}         
//	    		});
//	    	viewLink.setSortable(false);
//	    	viewLink.setFilterable(false);
//	    	htmlRow.addColumn(viewLink); 
	
			tableModel.setTable(htmlTable);
	
			String html = tableModel.render();
			return html;
		}
	}
	
	public String generateForRepairAssets(HttpServletRequest request, HttpServletResponse response){
		/**MOCK DATA**/
		ArrayList<TestObject> data = new ArrayList<TestObject>();
		TestDao tDao = new TestDao();
		data = tDao.retrieveAll();
		/**MOCK DATA**/
		
		TableModel tableModel = new TableModel("repair", request, response);
		
		tableModel.setExportTypes(ExportType.CSV,ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(data);
		
		if(tableModel.isExporting()){
			Table Table = new Table().caption("TestObject Record");
			
			Row Row = new Row();
			Table.setRow(Row);
			
			Column col1 = new Column("id").title("No.");
			Row.addColumn(col1);
	
			Column col2 = new Column("att1").title("Asset Tag");
			Row.addColumn(col2);
	
//			Column col3 = new Column("att2").title("Adapter Tag");
//			Row.addColumn(col3);
	
			Column col5 = new Column("att1").title("Description");
			Row.addColumn(col5);
	
			Column col6 = new Column("att2").title("Previous Assignee");
			Row.addColumn(col6);
	
			Column col7 = new Column("att3").title("Project Name");
			Row.addColumn(col7); 
	
			Column col8 = new Column("att1").title("Return Date");
			Row.addColumn(col8);
	
			Column col9 = new Column("att2").title("Repair Date");
			Row.addColumn(col9);
	
			Column col10 = new Column("att1").title("Release Date");
			Row.addColumn(col10);
	
			Column col11 = new Column("att2").title("Remarks");
			Row.addColumn(col11);
	
			tableModel.setTable(Table);
			
			String export = tableModel.render();
			return export;
		}
		else{
			HtmlTable htmlTable = new HtmlTable();
	
			HtmlRow HtmlRow = new HtmlRow();
			htmlTable.setRow(HtmlRow);
						
			HtmlColumn col1 = new HtmlColumn("id").title("No.");
			HtmlRow.addColumn(col1);
	
			HtmlColumn col2 = new HtmlColumn("att1").title("Asset Tag");
			HtmlRow.addColumn(col2);
	
//			HtmlColumn col3 = new HtmlColumn("att2").title("Adapter Tag");
//			HtmlRow.addColumn(col3);
	
			HtmlColumn col5 = new HtmlColumn("att1").title("Description");
			HtmlRow.addColumn(col5);
	
			HtmlColumn col6 = new HtmlColumn("att2").title("Previous Assignee");
			HtmlRow.addColumn(col6);
	
			HtmlColumn col7 = new HtmlColumn("att3").title("Project Name");
			HtmlRow.addColumn(col7); 
	
			HtmlColumn col8 = new HtmlColumn("att1").title("Return Date");
			HtmlRow.addColumn(col8);
	
			HtmlColumn col9 = new HtmlColumn("att2").title("Repair Date");
			HtmlRow.addColumn(col9);
	
			HtmlColumn col10 = new HtmlColumn("att1").title("Release Date");
			HtmlRow.addColumn(col10);
	
			HtmlColumn col11 = new HtmlColumn("att2").title("Remarks");
			HtmlRow.addColumn(col11);
	    	
//	    	HtmlColumn viewLink = new HtmlColumn("id").title("View");
//	    	viewLink.setCellEditor(new CellEditor() {             
//				public Object getValue(Object item, String property, int rowcount) {                 
//	    			//Object value = new HtmlCellEditor().getValue(item, property, rowcount);                 
//	    			HtmlBuilder html = new HtmlBuilder();
//	    			ExamResponse er = (ExamResponse)item;
//	    			html.a().href().quote().append("JMesaExportStudentExamServlet?id="+er.getExamResponseID()).quote().close();
//	    			html.append("View Response");
//	    			html.aEnd();
//	    			return html.toString();             
//	    			}         
//	    		});
//	    	viewLink.setSortable(false);
//	    	viewLink.setFilterable(false);
//	    	htmlRow.addColumn(viewLink); 
	
			tableModel.setTable(htmlTable);
	
			String html = tableModel.render();
			return html;
		}
	}

	public String generateOnStockAssets(HttpServletRequest request, HttpServletResponse response){
		/**MOCK DATA**/
		ArrayList<TestObject> data = new ArrayList<TestObject>();
		TestDao tDao = new TestDao();
		data = tDao.retrieveAll();
		/**MOCK DATA**/
		
		TableModel tableModel = new TableModel("stock", request, response);
		
		tableModel.setExportTypes(ExportType.CSV,ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(data);
		
		if(tableModel.isExporting()){
			Table Table = new Table().caption("TestObject Record");
			
			Row Row = new Row();
			Table.setRow(Row);
			
			Column col1 = new Column("id").title("No.");
			Row.addColumn(col1);
	
			Column col2 = new Column("att1").title("Asset Tag");
			Row.addColumn(col2);
	
//			Column col3 = new Column("att2").title("Adapter Tag");
//			Row.addColumn(col3);
	
			Column col5 = new Column("att1").title("Description");
			Row.addColumn(col5);
	
			Column col11 = new Column("att2").title("Remarks");
			Row.addColumn(col11);
	
			tableModel.setTable(Table);
			
			String export = tableModel.render();
			return export;
		}
		else{
			HtmlTable htmlTable = new HtmlTable();
	
			HtmlRow HtmlRow = new HtmlRow();
			htmlTable.setRow(HtmlRow);
						
			HtmlColumn col1 = new HtmlColumn("id").title("No.");
			HtmlRow.addColumn(col1);
	
			HtmlColumn col2 = new HtmlColumn("att1").title("Asset Tag");
			HtmlRow.addColumn(col2);
	
//			HtmlColumn col3 = new HtmlColumn("att2").title("Adapter Tag");
//			HtmlRow.addColumn(col3);
	
			HtmlColumn col5 = new HtmlColumn("att1").title("Description");
			HtmlRow.addColumn(col5);
	
			HtmlColumn col11 = new HtmlColumn("att2").title("Remarks");
			HtmlRow.addColumn(col11);
	    	
//	    	HtmlColumn viewLink = new HtmlColumn("id").title("View");
//	    	viewLink.setCellEditor(new CellEditor() {             
//				public Object getValue(Object item, String property, int rowcount) {                 
//	    			//Object value = new HtmlCellEditor().getValue(item, property, rowcount);                 
//	    			HtmlBuilder html = new HtmlBuilder();
//	    			ExamResponse er = (ExamResponse)item;
//	    			html.a().href().quote().append("JMesaExportStudentExamServlet?id="+er.getExamResponseID()).quote().close();
//	    			html.append("View Response");
//	    			html.aEnd();
//	    			return html.toString();             
//	    			}         
//	    		});
//	    	viewLink.setSortable(false);
//	    	viewLink.setFilterable(false);
//	    	htmlRow.addColumn(viewLink); 
	
			tableModel.setTable(htmlTable);
	
			String html = tableModel.render();
			return html;
		}
	}
	
}
